const BASE64TABLE = [
  'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
  'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
  'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
  'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
  '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'
];

export function decodeBase64MAC(hex) {
  const str = hex;
  const base64MAC = stringToByte(str);
  console.log("加密: " + str);

  let decodedMac = new Array(6);
  decodedMac = base64TomacAddrTo(base64MAC);
  const result = byteArrayToHexString(decodedMac).replace(/ /g,"");
  console.log("解密: " + result);
}

function macAddrTobase64(macAddr) {
  const base64MAC = new Array(8);

  let t = ((macAddr[0] & 0xFF) << 16) | ((macAddr[1] & 0xFF) << 8) | (macAddr[2] & 0xFF);
  base64MAC[0] = BASE64TABLE[t >> 18];
  base64MAC[1] = BASE64TABLE[(t >> 12) & 0x3F];
  base64MAC[2] = BASE64TABLE[(t >> 6) & 0x3F];
  base64MAC[3] = BASE64TABLE[t & 0x3F];

  t = ((macAddr[3] & 0xFF) << 16) | ((macAddr[4] & 0xFF) << 8) | (macAddr[5] & 0xFF);
  base64MAC[4] = BASE64TABLE[t >> 18];
  base64MAC[5] = BASE64TABLE[(t >> 12) & 0x3F];
  base64MAC[6] = BASE64TABLE[(t >> 6) & 0x3F];
  base64MAC[7] = BASE64TABLE[t & 0x3F];

  return base64MAC;
}

function base64TomacAddrTo(base64MAC) {
  const macAddr = new Array(6);

  let v = searchBase64Index(base64MAC[0]);
  let t = v;
  v = searchBase64Index(base64MAC[1]);
  t = (t << 6) | v;
  v = searchBase64Index(base64MAC[2]);
  t = (t << 6) | v;
  v = searchBase64Index(base64MAC[3]);
  t = (t << 6) | v;
  const decodedMac1 = [
    (t >> 16),
    (t >> 8),
    t
  ];
  console.log("Decoded MAC1: " + byteArrayToHexString(decodedMac1));

  v = searchBase64Index(base64MAC[4]);
  t = v;
  v = searchBase64Index(base64MAC[5]);
  t = (t << 6) | v;
  v = searchBase64Index(base64MAC[6]);
  t = (t << 6) | v;
  v = searchBase64Index(base64MAC[7]);
  t = (t << 6) | v;
  const decodedMac2 = [
    (t >> 16),
    (t >> 8),
    t
  ];
  console.log("Decoded MAC2: " + byteArrayToHexString(decodedMac2));

  macAddr[0] = decodedMac1[0];
  macAddr[1] = decodedMac1[1];
  macAddr[2] = decodedMac1[2];

  macAddr[3] = decodedMac2[0];
  macAddr[4] = decodedMac2[1];
  macAddr[5] = decodedMac2[2];

  console.log("MAC: " + byteArrayToHexString(macAddr));

  return macAddr;
}

function searchBase64Index(value) {
  for (let index = 0; index < BASE64TABLE.length; index++) {
    if (value === BASE64TABLE[index]) {
      return index;
    }
  }
  return -1;
}

function byteArrayToHexString(array) {
  let result = '';
  for (const b of array) {
    result += ("0" + (b & 0xFF).toString(16)).slice(-2).toUpperCase() + ' ';
  }
  return result.trim();
}

function stringToByte(str) {
  let bytes = null;

  try {
    bytes = hexToBytes(str);

    // 打印字节数组的内容
    for (const b of bytes) {
      console.log(b + " ");
    }
  } catch (error) {
    console.error(error);
  }

  return bytes;
}

function hexToBytes(hex) {
  const bytes = new Array(Math.ceil(hex.length / 2));
  for (let i = 0; i < bytes.length; i++) {
    bytes[i] = parseInt(hex.substr(i * 2, 2), 16);
  }
  return bytes;
}

